{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "1. 交叉熵损失函数样例ipynb",
      "version": "0.3.2",
      "provenance": [],
      "private_outputs": true,
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yejianfeng2014/AI/blob/master/book/chapter01/1_%E4%BA%A4%E5%8F%89%E7%86%B5%E6%8D%9F%E5%A4%B1%E5%87%BD%E6%95%B0%E6%A0%B7%E4%BE%8Bipynb.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "p8vCPpIJ0rZt",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import tensorflow as tf \n",
        "\n",
        "print(tf.__version__)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "-KDtgiE88Noy",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "###sparse_softmax_cross_entropy_with_logits样例"
      ]
    },
    {
      "metadata": {
        "id": "-_FV0JxV06Fy",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "#假设词汇标的数大小是3 ，语料包含两个单词 ‘0,2’\n",
        "word_label = tf.constant([2,0])\n",
        "# 假设模型对两个单词预测时，产生的logit分别是[2.0, -1.0, 3.0]和[1.0, 0.0, -0.5]\n",
        "\n",
        "predict_logs = tf.constant([[2.0,-1.0,3.0],[1.0,0.0,-0.5]])\n",
        "\n",
        "# 使用sparse_softmax_cross_entropy_with_logits计算交叉熵。\n",
        "\n",
        "loss = tf.nn.sparse_softmax_cross_entropy_with_logits (labels = word_label,logits = predict_logs)\n",
        "\n",
        "# 运行程序，计算loss的结果是[0.32656264, 0.46436879], 这对应两个预测的\n",
        "# perplexity损失。\n",
        "with tf.Session() as sess:\n",
        "  print(sess.run(loss))\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "88nAZ8RN-1TD",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "#### 2.  softmax_cross_entropy_with_logits样例。\n"
      ]
    },
    {
      "metadata": {
        "id": "dRMHaNvJ-h7r",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# softmax_cross_entropy_with_logits与上面的函数相似，但是需要将预测目标以\n",
        "# 概率分布的形式给出。\n",
        "\n",
        "word_prob_distribution = tf.constant([[0.0,0.0,1.0],[1.0,0.0,0.0]])\n",
        "\n",
        "loss = tf.nn.softmax_cross_entropy_with_logits(labels = word_prob_distribution, logits= predict_logs)\n",
        "# 运行结果与上面相同：[ 0.32656264,  0.46436879]\n",
        "with tf.Session() as sess:\n",
        "  \n",
        "  print(sess.run(loss))\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Tq_TbBMbADkB",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}